﻿using System.Linq;
using MSAcademic.v2.Database;

namespace MSAcademic.v2.Infrastructure.Gallery.Impls
{
    public class GalleryCommandsImpl : IGalleryCommands
    {
        public int Create(string name,bool isHidden)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var gallery = new Database.Gallery { Name = name, IsHidden = isHidden };
                db.Galleries.Add(gallery);
                db.SaveChanges();
                var id = gallery.Id;
                return id;
            }
        }
        public void Edit(int galleryId, string galleryName, bool galleryIsHidden)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var gallery = db.Galleries.SingleOrDefault(m => m.Id == galleryId);
                if (gallery != null)
                {
                    gallery.Name = galleryName;
                    gallery.IsHidden = galleryIsHidden;
                }
                db.SaveChanges();
            }
        }
        public void Attach(int articleId, int galleryId)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var article = db.Articles.SingleOrDefault(m => m.Id == articleId);
                var gallery = db.Galleries.SingleOrDefault(m => m.Id == galleryId);
                if (article != null && gallery != null)
                {
                    article.Gallery = gallery;
                    db.SaveChanges();
                }
            }

        }
        
    }

}
